<?php
/**
 * The make file for EUI Js. Returns the complete Embrace UI Javascript Library.
 * 
 * Can be used for generating once or generating dynamicly.
 * 
 * used processes:
 * 	- specify the files					=>	here all files within javascript/
 *  - write all files into one buffer	=> 	returning only one file
 *  - compress the buffer with JSMinify	=>	Removes comments and other overhead
 *  - echoing buffer using GZIP			=> 	reduce the transfer data for less traffic
 *  
 *  @author embrace.toolkit
 *  @version $Id: eui.js.php 85 2009-09-29 11:33:31Z embrace.toolkit $
 */

require_once("../external/php/packer/class.JavaScriptPacker.php");

/**
 * Returns a js comment block
 * 	/**
 *   * Start: filename
 *   *
 * @param $filenameName of the script.
 * @return String the comment block
 */
function getJsHeading($filename) {
	return getComment("Start: " . $filename);	
}

/**
 * Returns a js comment block
 * 	/**
 *   * Finished: filename
 *   *
 * @param $filenameName of the script.
 * @return String the comment block
 */
function getJsTrailing($filename) {
	return getComment("Finished: " . $filename);
}

/**
 * Writes any comment line to the javascript buffer.
 * 
 * @param $message String - the message to write into the 
 * buffer as comment.
 * 
 * @return String - a JS comment block
 */
function getComment($message) {
	$msg = "\n
		/**\n
		 * " . $message . "\n
		 */\n
	";
	return $msg;
}

$arrJsFiles = Array();
$jsBuffer = false;
$jsUncompressed = false;

/**
 * Detecting all needed JS Files.
 */
$path = "javascript/";

$files = scandir("javascript");

foreach($files as $file) {
	if(is_file($path . $file)) {
		$arrJsFiles[] = $path . $file;
	}
}

//$arrJsFiles[] = "../external/javascript/prototype.js";

/**
 * now put all files into a buffer and write some lines of code to the
 * buffer, in production there are no comments.
 */
foreach($arrJsFiles as $script) {
	$jsBuffer .= getJsHeading($script);
	$jsBuffer .= file_get_contents($script);
	$jsBuffer .= getJsTrailing($script);
}

if(!isset($_REQUEST["debug"])) {
	$packer = new JavaScriptPacker($jsBuffer);
	$jsBuffer = getJsHeading("Compressed content.");
	$jsBuffer .= $packer->pack();
	$jsBuffer .= getJsTrailing("Compressed content.");
}

$jsUncompressed .= getJsHeading("Uncompressed content(compatiblity).");
$jsUncompressed .= file_get_contents("../external/javascript/prototype.js");
$jsUncompressed .= getJsTrailing("Uncompressed content(compatiblity).");

if(!isset($_REQUEST["nogzip"])) {
	if(!ob_start("ob_gzhandler"))
		ob_start();
	else
		header("Content-Encoding: gzip");
}
echo $jsUncompressed;
echo $jsBuffer;
?>